package com.itheima.controller;

import com.itheima.pojo.Article;
import com.itheima.pojo.Book;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.vo.BookVo;
import com.itheima.service.ArticleService;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     *新增图书
     */
    @PostMapping
    public Result add(@RequestBody @Validated Book book) {
        bookService.addBook(book);
        return Result.success();
    }

    /**
     *分页查询图书
     */
    @GetMapping
    public Result<PageBean<BookVo>> list(
            @RequestParam(required = false ,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "100")Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) Integer bookshelfId,
            @RequestParam(required = false) String name
    ) {
        PageBean<BookVo> pb =  bookService.list(pageNum,pageSize,categoryId,bookshelfId,name);
        return Result.success(pb);
    }
    /**
     * 更新文章
     */
    @PutMapping
    public Result update(@RequestBody Book book){
        int a=1;
        bookService.updateBook(book);
        return Result.success();
    }

    /**
     * 删除文章
     */
    @DeleteMapping
    public Result delete(@RequestParam Integer id){
        bookService.deleteBook(id);
        return Result.success();
    }
}
